package com.booking.controller;

import com.booking.common.dto.PageReq;
import com.booking.common.dto.PageResp;
import com.booking.common.dto.ClientResp;
import com.booking.controller.model.req.order.OrderPageQueryReq;
import com.booking.controller.model.req.order.RoomOrderCancelReq;
import com.booking.controller.model.req.order.RoomOrderCommentReq;
import com.booking.controller.model.req.order.RoomOrderCreateReq;
import com.booking.controller.model.res.RoomOrderDetail;
import com.booking.dao.entity.RoomOrder;
import com.booking.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Qikai.Lin
 * @date 2023/4/24
 */
@Slf4j
@RestController
@RequestMapping("/api/roomOrder")
public class RoomOrderController {
    @Autowired
    private OrderService orderService;
    /* ----------------------房间订单---------------------- */

    /**
     * 查看所有房间订单（游客根据userId查询，房东根据homestayId查询）
     * @param request 分页筛选条件
     * @return 所有房间订单
     */
    @PostMapping("/page/getAll")
    public ClientResp<PageResp<RoomOrderDetail>> getAll(@RequestBody @Valid PageReq<OrderPageQueryReq> request) {
        return ClientResp.success(orderService.getRoomOrderPage(request));
    }

    /**
     * 查看房间订单详情
     * @param orderId 房间订单号
     * @return 房间订单详情
     */
    @PostMapping("/detail")
    public ClientResp<RoomOrderDetail> detail(@RequestParam Long orderId) {
        return ClientResp.success(orderService.getRoomOrderDetail(orderId));
    }
    //创建房间订单
    @PostMapping("/create")
    public ClientResp<RoomOrder> create(@RequestBody @Valid RoomOrderCreateReq request) {
        return ClientResp.success(orderService.createRoomOrder(request));
    }
    //取消房间订单
    @PostMapping("/cancel")
    public ClientResp<String> cancel(@RequestBody @Valid RoomOrderCancelReq request) {
        orderService.cancelRoomOrder(request);
        return ClientResp.success();
    }
    //终止房间订单
    @PostMapping("/shutdown")
    public ClientResp<String> shutdown(@RequestParam Long orderId) {
        orderService.shutdownRoomOrder(orderId);
        return ClientResp.success();
    }
    //评价房间订单
    @PostMapping("/comment")
    public ClientResp<String> comment(@RequestBody @Valid RoomOrderCommentReq request) {
        orderService.commentRoomOrder(request);
        return ClientResp.success();
    }

    //房间入住
    @PostMapping("/check")
    public ClientResp<String> check(@RequestParam Long orderId) {
        orderService.checkRoomOrder(orderId);
        return ClientResp.success();
    }
    //完成房间订单
    @PostMapping("/finish")
    public ClientResp<String> finish(@RequestParam Long orderId) {
        orderService.finishRoomOrder(orderId);
        return ClientResp.success();
    }
    //支付房间订单定金
    @PostMapping("/bookingPay")
    public ClientResp<String> bookingPay(@RequestParam Long orderId) {
        orderService.payRoomOrderBooking(orderId);
        return ClientResp.success();
    }
    //支付房间订单
    @PostMapping("/pay")
    public ClientResp<String> pay(@RequestParam Long orderId) {
        orderService.payRoomOrder(orderId);
        return ClientResp.success();
    }
}
